/**
 * Created by zhangzhihao on 2016/12/06 11:00;
 */
var vm = new Vue({
    el: '#main',
    data: {
    	roleDatas: {},
        pagination: [],
        queryData: {
            nowPage: 1,
            rowNum: 10,
            object:{
            	optNo:App.getParam("optNo")
            }
        },
        pageNum:null,
        userInfo:{
       		optNo:App.getParam("optNo")
       	},
		userRoles:{
			datas:[],
			object:{
				optNo:App.getParam("optNo")
			}
		},
		optTypes:[]
    },
    created: function () {
    	this.queryUserType();
    	
    },
    methods: {
        selectPage: function (page) {
            this.queryData.nowPage = page;
            this.queryRoles();
        },
        offsetPage: function (offset) {
            this.queryData.nowPage = this.queryData.nowPage + offset;
            this.queryRoles();
        },
        queryUserType:function(){
        	App.ajax({
    			type:"GET",
    			url:App.getApiUrl("queryUserTypeURL"),
    			success:function(data){
    				vm.optTypes = data;
    				vm.queryUserInfo();
				}
			});
        },
        queryUserInfo:function(){
        	App.ajax({
    			type:"GET",
    			url:App.getApiUrl("getUserDetailURL")+ "?optNo=" + this.userInfo.optNo,
    			success:function(data){
    				vm.userInfo = data;
    				vm.queryData.object.optType = data.optType;
    				setTimeout(function(){
    					selectSet("optType",data.optType);
    				}, 1);
    				
    				vm.queryRoles();
    			}
    		});
        },
        queryRoles:function(reset){
        	if(reset){
				this.queryData.nowPage = 1;
			}
        	App.ajax({
    			type:"POST",
    			url:App.getApiUrl("queryUserRolesURL"),
    			data:this.queryData,
    			success:function(data){
    				vm.roleDatas = data;

                    vm.queryData.nowPage = vm.roleDatas.nowPage;
                    vm.queryData.allPages = vm.roleDatas.allPages;
                    vm.pagination = getPagination(vm.queryData.nowPage,vm.queryData.allPages);
    			}
    		});
        },
        //全选按钮点击事件
        allBtn: function () {
            var checkBoxs = $(".anyCheck");
            var checked = $("#allCheck")[0].checked;
            if (checked) {
                for (var i = 0; i < checkBoxs.length; i++) {
                    checkBoxs[i].checked = true;
                }
            } else {
                for (var i = 0; i < checkBoxs.length; i++) {
                    checkBoxs[i].checked = false;
                }
            }
        },
        //返回上一页的点击事件的函数
        goBtn: function () {
            //不保留
            history.back(-1);
        },
        saveRoles:function(){
        	this.userRoles.datas = this.roleDatas.datas;

        	App.ajax({
    			type:"POST",
    			url:App.getApiUrl("addUserRoleURL"),
    			data:this.userRoles,
    			success:function(data){
    				parent.showAlert(data.msg,function(){
						window.location.reload();
					});
    			}
    		});
        }
    }
});

function rowNumChange(value){
    vm.queryData.rowNum = value;
    vm.queryData.nowPage = 1;
    vm.queryRoles();
}